﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using BusinessObjects;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class NamHocListView : Form
    {
        private BindingList<NamHoc> bindingListNamHoc;
        private NamHoc _namHoc;

        public NamHocListView()
        {
            InitializeComponent();
        }
        private void NamHocListView_Load(object sender, EventArgs e)
        {
            string[] listFieldName = new string[] { "TuNam", "DenNam", "TenNamHoc" };
            string[] listColumnName = new string[] { "Từ Năm", "Đến Năm", "Tên Năm Học" };
            AppGridView.InitGridView(gvNamHoc, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, true);
            AppGridView.ReadOnlyGridView(gvNamHoc);
            AppGridView.ShowField(gvNamHoc, listFieldName, listColumnName);

            LoadData();
        }

        private void LoadData()
        {
            bindingListNamHoc = new BindingList<NamHoc>(DataAccessObject.NamHoc.GetListNamHoc());

            bindingSourceNamHoc.DataSource = bindingListNamHoc;
            gvNamHoc.Invalidate();
        }


        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _namHoc = new NamHoc();
            NamHocDetailView detailView = new NamHocDetailView(_namHoc);
            OpenDetailForm(detailView);
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteRow();
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnLamMoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

        private void gcNamHoc_DoubleClick(object sender, EventArgs e)
        {
            _namHoc = gvNamHoc.GetFocusedRow() as NamHoc;
            if (_namHoc.Oid > 0)
            {
                NamHocDetailView detailView = new NamHocDetailView(_namHoc);
                OpenDetailForm(detailView);
            }
        }
        void OpenDetailForm(NamHocDetailView detailView)
        {
            detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
            detailView.DeleteChanged += new EventHandler(detailView_DeleteChanged);
            detailView.ShowDialog();
        }

        void detailView_DeleteChanged(object sender, EventArgs e)
        {
            DeleteRow();
        }

        private void DeleteRow()
        {
            if (MessageBox.Show("Bạn có chắc xóa?", "Xóa một dòng thông tin học kỳ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _namHoc = gvNamHoc.GetFocusedRow() as NamHoc;
                if (_namHoc != null && _namHoc.Oid > 0)
                {
                    if (DataAccessObject.NamHoc.Delete(_namHoc) > 0)
                    {
                        gvNamHoc.DeleteSelectedRows();
                        gvNamHoc.Invalidate();
                    }
                    else
                        MessageBox.Show("Không xóa được", "Có Lỗi Xảy Ra", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        void detailView_SaveChanged(object sender, EventArgs e)
        {
            bindingListNamHoc.Add(_namHoc);
            gvNamHoc.Invalidate();
        }

    }
}
